import GameManager from "../common/GameManager";
import SceneCom from "./SceneCom";
import { GameConfig } from "../common/GameConfig";
import Facebook from "../Facebook/Facebook";
import Logger from "../Logger/Logger";

const { ccclass, property } = cc._decorator;
@ccclass
export default class SceneManager extends cc.Component {

    @property(cc.Node)
    gameScene: cc.Node;     //游戏场景

    @property(cc.Node)
    loadingScene: cc.Node;     //加载资源场景

    
    @property(cc.Label)
    label: cc.Label;     

    private gameSceneCom: SceneCom;
    private loadingSceneCom: SceneCom;

    private curScene: SceneCom;

    private curSceneType;

    public static SceneType = {
        LoadingScene: 2,
        GameScene: 3,
    };

    public static SceneSwitchType = {
        RightToLeft: 1,
        LeftToRight: 2,
        UpToDown: 3,
        DownToUp: 4,
    };

    private compatibleBrowser() {
        if (cc.sys.isBrowser) {
            Logger.log("compatibleBrowser");
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }
    }

    onLoad() {
        
        this.compatibleBrowser();

        Facebook.Init();
        // Facebook.FBRank.GetGlobalRank();
        Facebook.FBRank.GetFriendRank();
        
        GameManager.getInstance().sceneManager = this;
        this.gameSceneCom = this.gameScene.getComponent("GameScene");
        this.loadingSceneCom = this.loadingScene.getComponent("LoadingScene");

        var width = this.node.width;
        var height = this.node.height;

        this.gameScene.width = width;
        this.loadingScene.width = width;

        this.gameScene.height = height;
        this.loadingScene.height = height;

        this.curSceneType = 0;
    }

    start() {
        this.gameScene.active = false;
        this.loadingScene.active = false;

        this.showScene(SceneManager.SceneType.LoadingScene);

    }

    //直接显示某个场景(没有切换效果)
    showScene(sceneType) {
        var nextScene = this.getSceneByType(sceneType);

        if (this.curScene == nextScene) {
            return;
        }

        if (this.curScene) {
            this.curScene.onHide();
        }
        if (nextScene) {
            this.curScene = nextScene;
            this.curScene.onShow();
        }

        this.curSceneType = sceneType;
    }

    switchScene(sceneType, switchType) {
        switchType = null; //屏蔽所有转场特效

        var nextScene = this.getSceneByType(sceneType);

        this.curSceneType = sceneType;

        if (!switchType) {
            this.showScene(sceneType);
        }

        switch (switchType) {
            case SceneManager.SceneSwitchType.RightToLeft:
                this.transitionRightToLeft(GameConfig.TransitionSceneDuration, nextScene);
                return;
                break;
            case SceneManager.SceneSwitchType.LeftToRight:
                this.transitionLeftToRight(GameConfig.TransitionSceneDuration, nextScene);
                return;
                break;
            case SceneManager.SceneSwitchType.UpToDown:
                this.transitionUpToDown(GameConfig.TransitionSceneDuration, nextScene);
                return;
                break;
            case SceneManager.SceneSwitchType.DownToUp:
                this.transitionDownToUp(GameConfig.TransitionSceneDuration, nextScene);
                return;
                break;
        }
    }

    //隐藏所有场景
    hideAllScene() {

    }

    getCurScene() {
        return this.curScene;
    }

    getCurSceneType() {
        return this.curSceneType;
    }

    getSceneByType(sceneType) {
        var scene = null;

        switch (sceneType) {
            case SceneManager.SceneType.GameScene: {
                scene = this.gameSceneCom;
                break;
            }

            case SceneManager.SceneType.LoadingScene: {
                scene = this.loadingSceneCom;
                break;
            }
        }
        return scene;
    }

    update(dt) {
        if (this.gameSceneCom && this.gameScene && this.gameScene.active) {
            this.gameSceneCom.frameOnMove(dt);
        }
        if (this.loadingSceneCom && this.loadingScene && this.loadingScene.active) {
            this.loadingSceneCom.frameOnMove(dt);
        }
    }


    //场景切换效果
    //从右到左移动
    transitionRightToLeft(duration, scene) {
        var self = this;

        scene.onShow();

        scene.node.position = cc.v2(cc.winSize.width, 0);

        var move = cc.moveTo(duration, cc.v2(0, 0));
        scene.node.runAction(cc.sequence(move, cc.callFunc(function () {
            self.curScene.onHide();
            self.curScene = scene;

        })));

        self.curScene.node.runAction(cc.moveBy(duration, cc.v2(-cc.winSize.width, 0)));
    }

    //从左到右移动
    transitionLeftToRight(duration, scene) {
        var self = this;

        scene.onShow();

        scene.node.position = cc.v2(-cc.winSize.width, 0);

        var move = cc.moveTo(duration, cc.v2(0, 0));
        scene.node.runAction(cc.sequence(move, cc.callFunc(function () {
            self.curScene.onHide();
            self.curScene = scene;

        })));

        self.curScene.node.runAction(cc.moveBy(duration, cc.v2(cc.winSize.width, 0)));
    }

    //从上至下移动
    transitionUpToDown(duration, scene) {
        var self = this;

        scene.onShow();

        scene.node.position = cc.v2(0, cc.winSize.height);

        var move = cc.moveTo(duration, cc.v2(0, 0));
        scene.node.runAction(cc.sequence(move, cc.callFunc(function () {
            self.curScene.onHide();
            self.curScene = scene;
        })));

        self.curScene.node.runAction(cc.moveBy(duration, cc.v2(0, -cc.winSize.height)));
    }

    //从下至上移动
    transitionDownToUp(duration, scene) {
        var self = this;

        scene.onShow();

        scene.node.position = cc.v2(0, -cc.winSize.height);

        var move = cc.moveTo(duration, cc.v2(0, 0));
        scene.node.runAction(cc.sequence(move, cc.callFunc(function () {
            self.curScene.onHide();
            self.curScene = scene;

        })));

        self.curScene.node.runAction(cc.moveBy(duration, cc.v2(0, cc.winSize.height)));
    }
}

